package com.wxtf.platform.main.contraceptives;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.ConstantInfoHelper;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import com.wxtf.platform.tools.FormatUtil;

/**
 * <li>Title: TmaProvide.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.contraceptives</li>
 * <li>Description: 药具发放领用登记明细</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Aug 21, 2012 10:29:02 AM</li>
 * 
 * @author zhanqiulin
 * @version 1.0.0.0
 * 
 */
public class TmaProvideDetail extends LogicBase implements CommonsCallProvider{
	private static final Log log = LogFactory.getLog(TmaProvideDetail.class);

	public TmaProvideDetail(String jsonArray){
		
	}
	
	@Override
	public Object run() {
		Object msg = "";
		try {
			if("queryPage".equals(this.getMethodName())) {
				msg = this.select(this.getLogicData());
				return msg;
			} else if("save".equals(this.getMethodName())) {
				msg = this.insert(this.getLogicData());
			} else if("update".equals(this.getMethodName())) {
				msg = this.update(this.getLogicData());
			} else if("delete".equals(this.getMethodName())) {
				msg = this.delete(this.getLogicData());
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}

			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	String[] fields = {"tmapd_id","tmad_id","tmapm_id","tmapd_num","reserve1","reserve2","reserve3","reserve4","reserve5","create_by","create_time","last_modify_by","last_modify_time"};
	@Override
	public Object select(Object jsonArray) throws Exception {
		Object[] params = null;
		StringBuffer querySql = new StringBuffer();
		sql = "from tma_provide_detail t left join tma_dictionary m on t.tmad_id = m.tmad_id ";
		JSONArray array = JSONArray.fromObject(jsonArray);
		String[] fields2 = {"tmapd_id","tmad_id","tmapm_id","tmapd_num","tmad_kind","tmad_name","tmad_spec","reserve1","reserve2","reserve3","reserve4","reserve5","create_by","create_time","last_modify_by","last_modify_time"};
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String tmapm_id = jsonObject.getString("tmapm_id");//
			StringBuffer buff = new StringBuffer();
			buff.append(" where 1=1 ");
			if(StringUtils.isNotBlank(tmapm_id)){
				buff.append(" and t.tmapm_id = '").append(tmapm_id).append("'");
			}
			sql += buff.toString();
		}
		String countSql = "select count(*) " + sql;
		querySql.append("select t.tmapd_id,t.tmad_id,t.tmapm_id,t.tmapd_num,m.tmad_kind,m.tmad_name,m.tmad_spec,t.reserve1,t.reserve2,t.reserve3,t.reserve4,t.reserve5,t.create_by,t.create_time,t.last_modify_by,t.last_modify_time ").append(sql)
		.append(" order by tmad_kind, tmad_name");
		return AccessDataBaseUtil.query4SQLPagging(fields2, countSql, querySql.toString(), this.getPage(), this.getPagesize(), params);
	}

	@Override
	public Object insert(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		
		StringBuffer buff = new StringBuffer();
		buff.append("insert into tma_provide_detail(tmapd_id");
		Object[] obj = new Object[fields.length];
		for (int j = 1; j < fields.length; j++) {
			obj[j] = jsonObject.getString(fields[j]);
			buff.append(",").append(fields[j]);
		}
		buff.append(") values(?,?,?,?,?,?,?,?,?,?,?,?,?);");
		obj[0] = AccessDataBaseUtil.getPKUUID();
		
		String curTime = FormatUtil.getCurentDateTime();
		obj[fields.length-4] = user.getUserId();
		obj[fields.length-3] = curTime;
		obj[fields.length-2] = user.getUserId();
		obj[fields.length-1] = curTime;
		
		AccessDataBaseUtil.executeSQLUpdate(buff.toString(), obj);
		return "\"保存成功\"";
	}

	@Override
	public Object update(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		String curTime = FormatUtil.getCurentDateTime();
		
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		StringBuffer buff = new StringBuffer();
		buff.append("update tma_provide_detail set ");
		Object[] obj = new Object[fields.length];
		for(int j = 1; j < fields.length; j++) {
			obj[j - 1] = jsonObject.getString(fields[j]);
			buff.append(fields[j]).append(" = ?,");
		}
		buff.deleteCharAt(buff.length() - 1).append(" where tmapd_id = ?");
		obj[fields.length-3] = user.getUserId();
		obj[fields.length-2] = curTime;
		obj[fields.length-1] = jsonObject.getString("tmapd_id");
		
		AccessDataBaseUtil.executeSQLUpdate(buff.toString(), obj);
		return "\"修改成功\"";
	}

	@Override
	public Object delete(Object jsonArray) throws Exception {
		JSONArray array = JSONArray.fromObject(jsonArray);
		JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
		String id = jsonObject.getString("ids");
		String[] ids = id.split(",");
		sql = "delete from tma_provide_detail where tmapd_id = ?";
		
		AccessDataBaseUtil.deleteByIds(sql, ids);
		return "\"删除成功\"";
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		return null;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}
	
}


